package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.FindWithDrawalAdapter;
import com.example.wanbaqian.adapter.LoadMoreAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.WithDrawalInfo;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 代理商提现界面
 */
public class FindWithDrawalListActivity extends BaseActivity {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    FindWithDrawalAdapter mAdapter;
    private List<WithDrawalInfo> mList = new ArrayList<>();
    @Override
    protected int getLayout() {
        return R.layout.activity_findbanklist;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.balck);
        setToolBar("提现记录");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        mAdapter=new FindWithDrawalAdapter(mContext, R.layout.item_findwithdrawallist, mList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setLoadMoreListener(new LoadMoreAdapter.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;getDate();
            }
        });
        swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDate();
            }
        });
        getDate();
    }

    private void getDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                        .getRequest(HttpURL.WITHDRAW_LOG, mContext,
                                new String[]{"accessid", "cur_page", "page_size"}
                                , new Object[]{SPUtils.getAccessid(mContext), page,
                                        count})
                , HttpURL.WITHDRAW_LOG, CommonValue.REQUEST_CODE1, new HttpCallback() {
                    @Override
                    public void onSuccess(String content, Info info, int requestCode) {
                        if (page == 1) {
                            mList.clear();
                        }
                        canLoadmore = JsonUtils.getInstance().havePage(content);
                        if(canLoadmore) mAdapter.setCanLoadMore(LoadMoreAdapter.STATE_LOAD_MORE);
                        else mAdapter.setCanLoadMore(LoadMoreAdapter.STATE_NO_MORE);

                        List<WithDrawalInfo> newmList =JsonUtils.getInstance().getList(content,"log_list",WithDrawalInfo.class);
                        mList.addAll(newmList);
                        mAdapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onCacheSuccess(String content, Info info, int requestCode) {
                        swipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Info info, int requestCode) {
                        swipeRefreshLayout.setRefreshing(false);
                        showToast(info.getMsg());
                    }
                });
    }
}
